package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/*
 *@AUTHOR:LIUCHAO;
 *@DATE: 2020/9/8 20:05
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;


    @Override
    public void delete(Integer id) {
        //查询当前检查组是否和套餐关联
        long count = checkGroupDao.findCountByCheckGroupId(id);
        if(count > 0){
            //当前检查项被引用，不能删除
            throw new RuntimeException("当前检查项被引用，不能删除");
        }
        checkGroupDao.deleteCheckGroupAndCheckItem(id);
        checkGroupDao.delete(id);
    }

    @Override
    public List<CheckGroup> findAll() {

        return checkGroupDao.findAll();
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
//        Integer checkGroupId = checkGroup.getId();
//        System.out.println(checkGroupId);
        checkGroupDao.add(checkGroup);
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
//        Integer  checkGroupId = checkGroup.getId();
//        System.out.println(checkGroupId);
       /* if (checkitemIds!=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroupId);
                map.put("checkitem_id",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }*/
    }
    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
//        Integer checkGroupId = checkGroup.getId();
//        System.out.println(checkGroupId);
        checkGroupDao.update(checkGroup);
        checkGroupDao.deleteCheckGroupAndCheckItem(checkGroup.getId());
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }


    private void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds!=null&&checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroupId);
                map.put("checkitem_id",checkitemId);
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }

    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page=checkGroupDao.selectByCondition(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        System.out.println("=========");
        List<Integer> checkItemIdsByCheckGroupId = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        for (Integer integer : checkItemIdsByCheckGroupId) {
            System.out.println(integer);
        }
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }




}
